Page({
  data: {
    // 后端请求URL
    url: '<-- 你的后端IP -->',

    // 发起扣款页面数据
    payOutTradeNo: '',
    payAuthNo: '',
    
    // 完结订单页面数据
    finishOutTradeNo: '',
    finishAuthNo: '',
    
    // 关闭订单页面数据
    closeOutTradeNo: '',
    closeAuthNo: '',

    // 订单退款
    refundOutTradeNo: '',
  },
  
  // 处理输入框变化
  onInputChange(e) {
    const { field } = e.currentTarget.dataset;
    this.setData({
      [field]: e.detail.value
    });
  },
  
  // 拉起芝麻免押受理台
  onPullPay() {
    my.request({
      url: this.data.url+'/alipay/freeze',
      method: 'POST',
      success: (res) => {
        my.tradePay({
          orderStr : res.data,
          success: (res) => {
            my.alert({
              title: '支付结果',
              content: JSON.stringify(res),
            });
          },
          fail: (e) => {
            my.alert({
              title: '支付失败',
              content: JSON.stringify(e),
            });
          }
        })
      },
      fail: (e) => {
        my.hideLoading();
        my.alert({
          title: '请求失败',
          content: JSON.stringify(e),
        });
      }
    });
  },
  
  //发起扣款 输入
  onPayOutTradeNo(e) {
    this.data.payOutTradeNo = e.detail.value;
  },
  onPayAuthNo(e) {
    this.data.payAuthNo = e.detail.value;
  },
  // 发起扣款
  onPay() {
    console.log(this.data)
    my.request({
      url: this.data.url+'/alipay/pay',
      data : {
        outTradeNo: this.data.payOutTradeNo,
        authNo: this.data.payAuthNo,
      },
      method: 'POST',
      success: (res) => {
        my.tradePay({
          success: (res2) => {
            tradeNo: res2.data.alipay_trade_pay_response.trade_no,
            my.alert({
              title: '支付结果',
              content: JSON.stringify(res),
            });
          },
          fail: (e) => {
            my.alert({
              title: '支付失败',
              content: JSON.stringify(e),
            });
          }
        })
      },
      fail: (e) => {
        my.hideLoading();
        my.alert({
          title: '请求失败',
          content: JSON.stringify(e),
        });
      }
    });
  },
  
  //关闭扣款 输入
  onCloseOutTradeNo(e) {
    this.data.closeOutTradeNo = e.detail.value;
  },
  // 关闭订单
  close() {
    // 模拟关闭订单请求
    my.request({
      url: this.data.url+'/alipay/close',
      method: 'POST',
      data: {
        tradeNo: this.data.closeOutTradeNo,
        authNo: this.data.closeAuthNo
      },
      success: (res) => {
        my.hideLoading();
        my.showToast({
          title: '订单已关闭',
          icon: 'success',
        });
      },
      fail: (e) => {
        my.hideLoading();
        my.alert({
          title: '关闭失败',
          content: JSON.stringify(e),
        });
      }
    });
  },
  
  
  //订单退款
  onRefundOutTradeNo(e) {
    this.data.refundOutTradeNo = e.detail.value;
  },
  // 订单退款
  refund() {
    // 模拟订单退款请求
    my.request({
      url: this.data.url+'/alipay/refund',
      method: 'POST',
      data: {
        outTradeNo: this.data.refundOutTradeNo,
      },
      success: (res) => {
        my.hideLoading();
        my.showToast({
          title: '订单已关闭',
          icon: 'success',
        });
      },
      fail: (e) => {
        my.hideLoading();
        my.alert({
          title: '关闭失败',
          content: JSON.stringify(e),
        });
      }
    });
  }
});
